using Microsoft.Extensions.Options;
using 高校员工餐饮信息管理系统.Configuration;

namespace 高校员工餐饮信息管理系统.Services
{
    /// <summary>
    /// 邮件服务实现
    /// </summary>
    public class EmailService : IEmailService
    {
        private readonly ILogger<EmailService> _logger;
        private readonly EmailSettings _emailSettings;
        
        public EmailService(ILogger<EmailService> logger, IOptions<EmailSettings> emailSettings)
        {
            _logger = logger;
            _emailSettings = emailSettings.Value;
        }
        
        public async Task<bool> SendPasswordResetEmailAsync(string toEmail, string resetLink)
        {
            _logger.LogInformation($"发送密码重置邮件: {toEmail}");
            
            var subject = "密码重置 - 高校员工餐饮信息管理系统";
            var body = $@"
                <html>
                <body>
                    <h2>密码重置请求</h2>
                    <p>您好，</p>
                    <p>我们收到了您的密码重置请求。请点击下面的链接重置您的密码：</p>
                    <p><a href='{resetLink}'>重置密码</a></p>
                    <p>如果您没有请求重置密码，请忽略此邮件。</p>
                    <br/>
                    <p>此致，</p>
                    <p>高校员工餐饮信息管理系统</p>
                </body>
                </html>
            ";
            
            return await SendEmailAsync(toEmail, subject, body);
        }
        
        public async Task<bool> SendNotificationEmailAsync(string toEmail, string subject, string body)
        {
            _logger.LogInformation($"发送通知邮件: {toEmail}, 主题: {subject}");
            
            return await SendEmailAsync(toEmail, subject, body);
        }
        
        private async Task<bool> SendEmailAsync(string toEmail, string subject, string body)
        {
            try
            {
                // TODO: 实际项目中应使用MailKit发送邮件
                // 这里提供模拟实现
                _logger.LogInformation($"发送邮件: {toEmail}");
                _logger.LogInformation($"SMTP服务器: {_emailSettings.SmtpServer}:{_emailSettings.Port}");
                _logger.LogInformation($"发件人: {_emailSettings.FromEmail}");
                
                await Task.Delay(500); // 模拟异步操作
                
                _logger.LogInformation("邮件发送成功");
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"邮件发送失败: {toEmail}");
                return false;
            }
        }
    }
}

